package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.IMenuItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.OrderItems;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 订单Service业务层处理
 * 
 * @author codernight
 * @date 2025-03-14
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private IMenuItemService menuItemService;



    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        orders.setCreateBy(getUsername());
        orders.setCreateById(getUserId());
        orders.setUserId(getUserId());
        orders.setStatus("pending");
        orders.setIsDelete(0);
        int rows = ordersMapper.insertOrders(orders);
        insertOrderItems(orders);
        return rows;
    }

    /**
     * 确认订单
     */
    @Override
    public int submitOrdersById(Long id)
    {
        Orders orders = ordersMapper.selectOrdersById(id);
        orders.setStatus("success");
        orders.setUpdateTime(DateUtils.getNowDate());
        orders.setUpdateBy(getUsername());
        orders.getOrderItemsList().forEach((x) -> {
            menuItemService.setCheckNumberById(x.getProductId(), x.getQuantity());
        });
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 修改订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrders(Orders orders)
    {
        orders.setUpdateTime(DateUtils.getNowDate());
        orders.setUpdateBy(getUsername());
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        ordersMapper.deleteOrderItemsByOrderIds(ids);
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrdersById(Long id)
    {
        ordersMapper.deleteOrderItemsByOrderId(id);
        return ordersMapper.deleteOrdersById(id);
    }

    /**
     * 新增订单详情信息
     * 
     * @param orders 订单对象
     */
    public void insertOrderItems(Orders orders)
    {
        List<OrderItems> orderItemsList = orders.getOrderItemsList();
        Long id = orders.getId();
        if (StringUtils.isNotNull(orderItemsList))
        {
            List<OrderItems> list = new ArrayList<OrderItems>();
            for (OrderItems orderItems : orderItemsList)
            {
                orderItems.setOrderId(id);
                list.add(orderItems);
            }
            if (list.size() > 0)
            {
                ordersMapper.batchOrderItems(list);
            }
        }
    }
}
